package com.leyou.favorite.controller;

import com.leyou.favorite.pojo.Favorite;
import com.leyou.favorite.service.FavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author CHERRY
 * @date 2018-09-21-11:26
 */
@Controller
public class FavoriteController {

    @Autowired
    private FavoriteService favoriteService;

    /**
     * 添加我的关注
     *
     * @param skuId 关注的商品id
     * @return
     */
    @PostMapping("{skuId}")
    public ResponseEntity<Void> addMyFavorite(@PathVariable("skuId") Long skuId) {

        this.favoriteService.addMyFavorite(skuId);

        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 取消我的关注
     *
     * @param skuId 关注的商品id
     * @return
     */
    @DeleteMapping("{skuId}")
    public ResponseEntity<Void> deleteMyFavorite(@PathVariable("skuId") Long skuId) {

        this.favoriteService.deleteMyFavorite(skuId);

        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 查询我的关注
     *
     * @return
     */
    @GetMapping
    public ResponseEntity<List<Favorite>> queryMyFavorite() {

        List<Favorite> favorites = this.favoriteService.queryMyFavorite();

        if (CollectionUtils.isEmpty(favorites)) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(favorites);
    }

    /**
     * 查询登录用户是否关注该商品
     *
     * @param skuId
     * @return
     */
    @GetMapping("state")
    public ResponseEntity<Boolean> queryIsFavorite(@RequestParam("id") Long skuId) {

        Boolean flag = this.favoriteService.queryIsFavorite(skuId);

        if (flag == null) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
        return ResponseEntity.ok(flag);
    }

    /**
     * 添加我的足迹
     *
     * @param
     * @return
     */
    @PostMapping("track")
    public ResponseEntity<Void> addMyTrack(@RequestBody Favorite favorite) {

        this.favoriteService.addMyTrack(favorite);

        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 查看历史足迹
     *
     * @return
     */
    @GetMapping("track")
    public ResponseEntity<List<Favorite>> queryMyTrack() {

        List<Favorite> favorites = this.favoriteService.queryMyTrack();

        if (CollectionUtils.isEmpty(favorites)) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(favorites);
    }

}
